﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using NerdDinnerDataComponent;



namespace NerdDinnerWCFService
{
    public class DinnerService : IDinnerService
    {
        private readonly IDinnerRepository _dinnerRepository;

        public DinnerService():this(new DinnerRepository())
        {
            
        }
        public DinnerService(IDinnerRepository dinnerRepository)
        {
            _dinnerRepository = dinnerRepository;
        }

        public PaginatedDinnerResultList FindUpcomingDinners(int pageIndex, int pageSize)
        {
           
           var pagList = _dinnerRepository.FindUpcomingDinners(pageIndex, pageSize);

            return new PaginatedDinnerResultList()
            {
                Result = Mapper.CreateFrom(pagList.Result),
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalCount = pagList.TotalCount,
                TotalPages = pagList.TotalPages,
                HasNextPage = pagList.HasNextPage,
                HasPreviousPage  =pagList.HasPreviousPage
            };
           
        }

        public DinnerDTO[] FindDinnersByText(string q)
        {
            return Mapper.CreateFrom(_dinnerRepository.FindDinnersByText(q));
        }

        public DinnerDTO[] FindAllDinners()
        {
            return Mapper.CreateFrom( _dinnerRepository.FindAllDinners());
        }

        public DinnerDTO GetDinner(int id)
        {
            var dinnerInfo=  _dinnerRepository.GetDinner(id);
            return Mapper.CreateFrom(dinnerInfo);
        }

      

        public void Delete(int dinnerId)
        {
            try
            {
                _dinnerRepository.Delete(dinnerId);
            }
            catch (ApplicationException ex)
            {
                throw new FaultException<NotFoundFault>(new NotFoundFault
                                                            {
                                                                Code = "1234",
                                                                Message = string.Format("dinner not found with tech id : {0}", dinnerId)
                                                            });
            }
          
          
         
        }



        public DinnerViewDTO[] FindByLocation(float latitude, float longitude)
        {
            var dinnerInfos = _dinnerRepository.FindByLocation(latitude, longitude);
            return Mapper.CreateFrom( dinnerInfos);
        }


        public void RegisterForDinner(int dinnerId, string userName)
        {
            try
            {
                _dinnerRepository.RegisterForDinner(dinnerId, userName);
            }
            catch (ApplicationException ex)
            {
                throw new FaultException<NotFoundFault>(new NotFoundFault
                {
                    Code = "1234",
                    Message = string.Format("dinner not found with tech id : {0}", dinnerId)
                });
            }
        }


        public int CreateDinner(DinnerDTO dinner)
        {
            var dinnerCreate = Mapper.CreateFrom((dinner));
            return _dinnerRepository.CreateDinner((dinnerCreate));
        }


        public void UpdateDinner(DinnerDTO dinner)
        {
            var dinnerToUpdated = Mapper.CreateFrom((dinner));
             try
            {
            _dinnerRepository.UpdateDinner((dinnerToUpdated));
            }
             catch (ApplicationException ex)
             {
                 throw new FaultException<NotFoundFault>(new NotFoundFault
                 {
                     Code = "1234",
                     Message = string.Format("dinner not found with tech id : {0}", dinner.DinnerId)
                 });
             }

       
           
           
        }
    }
}
